package com.mb.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbProductClass;
import com.mb.app.dao.TbProductClassMapper;
import com.mb.app.service.ITbProductClassService;
import com.mb.common.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品分类  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-04-01
 */
@Service
@Slf4j
public class TbProductClassServiceImpl extends ServiceImpl<TbProductClassMapper, TbProductClass> implements ITbProductClassService {

    @Autowired
    private CommonConfig commonConfig;

    @Autowired
    private ITbProductClassService productClassService;

    @Override
    public List<TbProductClass> queryList(Map<String, Object> map) {
        List<TbProductClass> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int save(TbProductClass TbProductClass) {
        TbProductClass.setCreateTime(new Date());
        if(StringUtils.isNotEmpty(TbProductClass.getPicUrl())) {
            String 	picUrl = TbProductClass.getPicUrl();
            picUrl = picUrl.replaceAll(commonConfig.getImgDomain(),"");
            TbProductClass.setPicUrl(picUrl);
        }
        if(StringUtils.isNotEmpty(TbProductClass.getLogoUrl())) {
            String 	logoUrl = TbProductClass.getLogoUrl();
            logoUrl = logoUrl.replaceAll(commonConfig.getImgDomain(),"");
            TbProductClass.setLogoUrl(logoUrl);
        }
        return this.baseMapper.insert(TbProductClass);
    }

    @Override
    public int update(TbProductClass TbProductClass) {
        try {
            TbProductClass TbProductClass1 = new TbProductClass();
            TbProductClass1.setClassId(TbProductClass.getClassId());
            TbProductClass1.setDataStatus(1);
            TbProductClass TbProductClassDO = this.baseMapper.selectOne(TbProductClass1);
            if(TbProductClassDO != null){
                if(StringUtils.isNotEmpty(TbProductClass.getPicUrl())) {
                    String 	picUrl = TbProductClass.getPicUrl();
                    picUrl = picUrl.replaceAll(commonConfig.getImgDomain(),"");
                    TbProductClass.setPicUrl(picUrl);
                }
                if(StringUtils.isNotEmpty(TbProductClass.getLogoUrl())) {
                    String 	logoUrl = TbProductClass.getLogoUrl();
                    logoUrl = logoUrl.replaceAll(commonConfig.getImgDomain(),"");
                    TbProductClass.setLogoUrl(logoUrl);
                }
                TbProductClass.setUpdateTime(new Date());
                this.baseMapper.updateById(TbProductClass);
            }
        } catch (Exception e) {
            log.error("update Exception",e);
        }
        return 0;
    }


    @Override
    public int remove(Long classId,Long delUserId) {
//        return this.baseMapper.deleteById(appId);
        List<TbProductClass> tbProductClasses = productClassService.selectClasss(classId);
        Integer integer = null;
        for (TbProductClass TbProductClass : tbProductClasses)
        {
            TbProductClass.setDeleteTime(new Date());
            TbProductClass.setDataStatus(0);
            TbProductClass.setDeleteUserId(delUserId);
            integer = this.baseMapper.updateById(TbProductClass);
        }
        return integer;
    }

    @Override
    public TbProductClass getDtl(Long classId) {
        TbProductClass TbProductClass = this.baseMapper.selectByIds(classId);
        if(TbProductClass != null && StringUtils.isNotEmpty(TbProductClass.getPicUrl())) {
            TbProductClass.setPicUrl(commonConfig.getImgDomain()+TbProductClass.getPicUrl());
        }
        if(TbProductClass != null && StringUtils.isNotEmpty(TbProductClass.getLogoUrl())) {
            TbProductClass.setLogoUrl(commonConfig.getImgDomain()+TbProductClass.getLogoUrl());
        }
        return TbProductClass;
    }

    @Override
    public boolean productClassNameExit(Map<String, Object> params) {
        boolean exit;
        exit = this.baseMapper.selectByProductClassInfo(params).size() > 0;
        return exit;
    }

    @Override
    public List<TbProductClass> getProductClassInfoList(Map<String, Object> map) {
        List<TbProductClass> productClasses = this.baseMapper.selectByProductClassInfo(map);
        return productClasses;
    }

    @Override
    public List<TbProductClass> selectClasss(Long classId)
    {
        List<TbProductClass> list = this.baseMapper.selectClasss(classId);
        return list;
    }

}
